///
/// [Author] Alex (https://github.com/AlexV525)
/// [Date] 2019-12-17 10:56
///
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterlib/common/style/theme_group.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/utils/utils.dart';


class ThemesProvider with ChangeNotifier {
  ThemesProvider() {
    initTheme();
  }

  ThemeGroup _currentThemeGroup = defaultThemeGroup;

  ThemeGroup get currentThemeGroup => _currentThemeGroup;

  set currentThemeGroup(ThemeGroup value) {
    assert(value != null);
    if (_currentThemeGroup == value) {
      return;
    }
    _currentThemeGroup = value;
    notifyListeners();
  }

  bool _dark = false;

  bool get dark => _dark;

  set dark(bool value) {
    assert(value != null);
    if (_dark == value) {
      return;
    }
    HiveFieldUtils.setBrightnessDark(value);
    _dark = value;
    notifyListeners();
  }

  bool _platformBrightness = true;

  bool get platformBrightness => _platformBrightness;

  set platformBrightness(bool value) {
    assert(value != null);
    if (_platformBrightness == value) {
      return;
    }
    HiveFieldUtils.setBrightnessPlatform(value);
    _platformBrightness = value;
    notifyListeners();
  }

  Future<void> initTheme() async {
    int themeIndex = HiveFieldUtils.getColorThemeIndex();
    if (themeIndex >= supportThemeGroups.length) {
      HiveFieldUtils.setColorTheme(0);
      themeIndex = 0;
    }
    _currentThemeGroup = supportThemeGroups[themeIndex];
    _dark = HiveFieldUtils.getBrightnessDark();
    _platformBrightness = HiveFieldUtils.getBrightnessPlatform();
  }

  void resetTheme() {
    HiveFieldUtils.setColorTheme(0);
    HiveFieldUtils.setBrightnessDark(false);
    HiveFieldUtils.setBrightnessPlatform(true);
    _currentThemeGroup = defaultThemeGroup;
    _dark = false;
    _platformBrightness = true;
    notifyListeners();
  }

  void updateThemeColor(int themeIndex) {
    HiveFieldUtils.setColorTheme(themeIndex);
    _currentThemeGroup = supportThemeGroups[themeIndex];
    notifyListeners();
  }

  void setSystemUIDark(bool isDark) {
    SystemChrome.setSystemUIOverlayStyle(
      isDark ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark,
    );
  }


  ThemeData get lightTheme {
    final Color currentColor = currentThemeGroup.lightThemeColor;
    final Color primaryColor = currentThemeGroup.lightPrimaryColor;
    final Color backgroundColor = currentThemeGroup.lightBackgroundColor;
    final Color dividerColor = currentThemeGroup.lightDividerColor;
    final Color primaryTextColor = currentThemeGroup.lightPrimaryTextColor;
    final Color secondaryTextColor = currentThemeGroup.lightSecondaryTextColor;
    final Color defaultLightColor = currentThemeGroup.lightPrimaryColor;
    return ThemeData.light().copyWith(
      platform: TargetPlatform.iOS,
      brightness: Brightness.light,
      primaryColor: primaryColor,//页面上使用最多的颜色
      primaryColorBrightness: Brightness.light,
      primaryColorLight: primaryColor,//使用最多的颜色这里又分了色系
      primaryColorDark: backgroundColor,
      accentColor: currentColor,//页面上使用第二多的颜色，一般是用于主题色
      accentColorBrightness: Brightness.light,
      canvasColor: backgroundColor,
      dividerColor: dividerColor,
      scaffoldBackgroundColor: backgroundColor,//页面的背景色
      bottomAppBarColor: primaryColor,
      cardColor: primaryColor,//一般选项卡的颜色
      highlightColor: Colors.transparent,
      splashFactory: const NoSplashFactory(),
      toggleableActiveColor: currentColor,
      cursorColor: currentColor,//编辑光标的颜色
      textSelectionColor: currentColor.withAlpha(100),//文字选中的颜色
      textSelectionHandleColor: currentColor,
      indicatorColor: currentColor,//选项卡的下标的颜色
      appBarTheme: const AppBarTheme(//定义appbar的样式,文字样式还是在text中定义比较好
        brightness: Brightness.light,
        elevation: 0,
      ),
      iconTheme: IconThemeData(color: secondaryTextColor),//icon的颜色,可以定义icon点击和未点击时候的颜色
      primaryIconTheme: IconThemeData(color: primaryColor),
      accentIconTheme: IconThemeData(color: currentColor),
      floatingActionButtonTheme: FloatingActionButtonThemeData(//定义悬浮按钮的颜色
        foregroundColor: primaryColor,
        backgroundColor: currentColor,
      ),
      tabBarTheme: TabBarTheme(//定义选项卡的颜色,大小,默认使用控件设置的颜色，如果没有设置使用这里配置的颜色
        indicatorSize: TabBarIndicatorSize.tab,
        labelColor: primaryTextColor,
        unselectedLabelColor: primaryTextColor,
      ),
      colorScheme: ColorScheme(//配置一些常用颜色
        primary: currentColor,
        primaryVariant: currentColor,
        secondary: currentColor,
        secondaryVariant: currentColor,
        surface: Colors.white,
        background: backgroundColor,
        error: defaultLightColor,
        onPrimary: currentColor,
        onSecondary: currentColor,
        onSurface: Colors.white,
        onBackground: backgroundColor,
        onError: defaultLightColor,
        brightness: Brightness.light,
      ),
      buttonColor: currentColor,//raisebutton的默认颜色
      textTheme: TextTheme(//一些常用的字体设置,主要是设置
          // NAME         SIZE  WEIGHT  SPACING
          // /// headline1    96.0  light   -1.5
          // /// headline2    60.0  light   -0.5
          // /// headline3    48.0  regular  0.0
          // /// headline4    34.0  regular  0.25
          // /// headline5    24.0  regular  0.0
          // /// headline6    20.0  medium   0.15
          // /// subtitle1    16.0  regular  0.15
          // /// subtitle2    14.0  medium   0.1
          // /// body1        16.0  regular  0.5   (bodyText1)
          // /// body2        14.0  regular  0.25  (bodyText2)
          // /// button       14.0  medium   1.25
          // /// caption      12.0  regular  0.4
          // /// overline     10.0  regular  1.5
        bodyText1: QuhuiConstant.smallSubText,
        bodyText2: QuhuiConstant.minText,
        button: QuhuiConstant.minSubText,
        caption: TextStyle(color: secondaryTextColor),
        subtitle1: TextStyle(color: secondaryTextColor),
        headline1: QuhuiConstant.largeTextBold,
        headline2: QuhuiConstant.largeTextWhiteBold,
        headline3: QuhuiConstant.middleTextBold,
        headline4: QuhuiConstant.middleText,
        headline5: QuhuiConstant.smallTextBold,
        headline6: QuhuiConstant.smallText,
        overline: TextStyle(color: primaryTextColor),
      ),
    );
  }

  ThemeData get darkTheme {
    final Color currentColor = currentThemeGroup.darkThemeColor;
    final Color primaryColor = currentThemeGroup.darkPrimaryColor;
    final Color backgroundColor = currentThemeGroup.darkBackgroundColor;
    final Color dividerColor = currentThemeGroup.darkDividerColor;
    final Color primaryTextColor = currentThemeGroup.darkPrimaryTextColor;
    final Color secondaryTextColor = currentThemeGroup.darkSecondaryTextColor;
    final Color defaultLightColor = currentThemeGroup.lightPrimaryColor;
    return ThemeData.dark().copyWith(
      brightness: Brightness.dark,
      primaryColor: primaryColor,
      primaryColorBrightness: Brightness.dark,
      primaryColorLight: backgroundColor,
      primaryColorDark: primaryColor,
      accentColor: currentColor,
      accentColorBrightness: Brightness.dark,
      canvasColor: backgroundColor,
      dividerColor: dividerColor,
      scaffoldBackgroundColor: backgroundColor,
      bottomAppBarColor: primaryColor,
      cardColor: primaryColor,
      highlightColor: Colors.transparent,
      splashFactory: const NoSplashFactory(),
      toggleableActiveColor: currentColor,
      cursorColor: currentColor,
      textSelectionColor: currentColor.withAlpha(100),
      textSelectionHandleColor: currentColor,
      indicatorColor: currentColor,
      appBarTheme: const AppBarTheme(
        brightness: Brightness.dark,
        elevation: 0,
      ),
      iconTheme: IconThemeData(color: secondaryTextColor),
      primaryIconTheme: IconThemeData(color: secondaryTextColor),
      floatingActionButtonTheme: FloatingActionButtonThemeData(
        foregroundColor: Colors.black,
        backgroundColor: currentColor,
      ),
      tabBarTheme: TabBarTheme(
        indicatorSize: TabBarIndicatorSize.tab,
        labelColor: primaryTextColor,
        unselectedLabelColor: primaryTextColor,
      ),
      colorScheme: ColorScheme(
        primary: currentColor,
        primaryVariant: currentColor,
        secondary: currentColor,
        secondaryVariant: currentColor,
        surface: primaryColor,
        background: backgroundColor,
        error: defaultLightColor,
        onPrimary: currentColor,
        onSecondary: currentColor,
        onSurface: primaryColor,
        onBackground: backgroundColor,
        onError: defaultLightColor,
        brightness: Brightness.dark,
      ),
      textTheme: TextTheme(
        bodyText1: TextStyle(color: secondaryTextColor),
        bodyText2: TextStyle(color: primaryTextColor),
        button: TextStyle(color: primaryTextColor),
        caption: TextStyle(color: secondaryTextColor),
        subtitle1: TextStyle(color: secondaryTextColor),
        headline1: TextStyle(color: secondaryTextColor),
        headline2: TextStyle(color: secondaryTextColor),
        headline3: TextStyle(color: secondaryTextColor),
        headline4: TextStyle(color: secondaryTextColor),
        headline5: TextStyle(color: primaryTextColor),
        headline6: TextStyle(color: primaryTextColor),
        overline: TextStyle(color: primaryTextColor),
      ),
      buttonColor: currentColor,
    );
  }
}

final List<ThemeGroup> supportThemeGroups = <ThemeGroup>[
  defaultThemeGroup, // This is the default theme group.
  const ThemeGroup(
    lightThemeColor: Color(0xfff06292),
    darkThemeColor: Color(0xffcc537c),
  ),
  const ThemeGroup(
    lightThemeColor: Color(0xffba68c8),
    darkThemeColor: Color(0xff9e58aa),
  ),
  const ThemeGroup(
    lightThemeColor: Color(0xff2196f3),
    darkThemeColor: Color(0xff1c7ece),
  ),
  const ThemeGroup(
    lightThemeColor: Color(0xff00bcd4),
    darkThemeColor: Color(0xff00a0b4),
  ),
  const ThemeGroup(
    lightThemeColor: Color(0xff26a69a),
    darkThemeColor: Color(0xff208d83),
  ),
  const ThemeGroup(
    lightThemeColor: Color(0xffffeb3b),
    darkThemeColor: Color(0xffd9c832),
    lightButtonTextColor: Colors.black,
    darkButtonTextColor: Colors.black,
  ),
  const ThemeGroup(
    lightThemeColor: Color(0xffff7043),
    darkThemeColor: Color(0xffd95f39),
  ),
];
